package com.info33.platform.business.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.info33.platform.business.app.dto.AppTenantApplyDTO;
import com.info33.platform.business.app.entity.AppTenantApply;
import com.info33.platform.business.app.service.AppTenantApplyService;
import com.info33.platform.common.constant.TableConstant;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.common.validated.UpdateValidated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author admin
 */
@RestController
@RequestMapping("/AppTenantApply")
@Api(tags = "app - 租户 - 申请记录")
public class AppTenantApplyController extends BaseController<AppTenantApplyService, AppTenantApply> {

    private final AppTenantApplyService appTenantApplyService;

    @Autowired
    public AppTenantApplyController(AppTenantApplyService appTenantApplyService) {
        super(appTenantApplyService);
        this.appTenantApplyService = appTenantApplyService;
    }

    @GetMapping(value = "/app/myLog/{pageNum}/{pageSize}")
    @ApiOperation(value = "app - 我的申请记录")
    public Result<IPage<AppTenantApply>> appMyLog(
            @PathVariable int pageNum,
            @PathVariable int pageSize) {
        return Result.ok(appTenantApplyService.page(
                new Page<>(pageNum, pageSize)
                , new QueryWrapper<AppTenantApply>()
                        .eq(TableConstant.CREATE_BY, getCurrentUser().getAppTenantUserVO().getId())
                        .orderByDesc(TableConstant.UPDATE_DATE)));
    }

    @GetMapping(value = "/pc/untreated/{pageNum}/{pageSize}")
    @ApiOperation(value = "pc - 待处理列表")
    public Result<IPage<AppTenantApply>> pcUntreated(
            @PathVariable int pageNum,
            @PathVariable int pageSize) {
        return Result.ok(appTenantApplyService.page(
                new Page<>(pageNum, pageSize)
                , new QueryWrapper<AppTenantApply>()
                        .eq("audit", TableConstant.STATUS_UNTREATED)
                        .orderByAsc(TableConstant.CREATE_DATE)));
    }

    @GetMapping(value = "/pc/history/{pageNum}/{pageSize}")
    @ApiOperation(value = "pc - 所有申请记录")
    public Result<IPage<AppTenantApply>> pcAll(
            @PathVariable int pageNum,
            @PathVariable int pageSize) {
        return Result.ok(appTenantApplyService.page(
                new Page<>(pageNum, pageSize)
                , new QueryWrapper<AppTenantApply>()
                        .ne("audit", TableConstant.STATUS_UNTREATED)
                        .orderByAsc("audit")
                        .orderByDesc(TableConstant.UPDATE_DATE)));
    }

    @PutMapping(value = "/pc/audit/refuse")
    @ApiOperation(value = "pc - 拒绝此条申请")
    public Result<Object> pcRefuse(
            @Validated(value = {UpdateValidated.class, AppTenantApplyDTO.PcAudit.class}) @RequestBody AppTenantApplyDTO dto) {
        AppTenantApply entity = new AppTenantApply();
        entity.setId(dto.getId());
        entity.setRemarks(dto.getRemarks());
        entity.setAudit(TableConstant.STATUS_FALSE);
        return Boolean.TRUE.equals(appTenantApplyService.updateById(entity)) ? Result.ok() : Result.error();
    }

    @PostMapping(value = "/pc/audit/agree")
    @ApiOperation(value = "pc - 同意此条申请")
    public Result<Object> pcAgree(
            @Validated(value = {UpdateValidated.class, AppTenantApplyDTO.PcAgree.class}) @RequestBody AppTenantApplyDTO dto) {
        return appTenantApplyService.pcAgree(dto);
    }
}